<template>
	<view class="index">
		<view class="accountSetUp">
			<view class="accountSetUpBox">
				<view @tap="navTo" class="accountSetUpBoxItem">
					<text>修改登录密码</text>
					<image src="@/static/img/zhankai.png" mode="widthFix"></image>
				</view>
				<view @tap="navToPayPw" class="accountSetUpBoxItem">
					<text>修改支付密码</text>
					<image src="@/static/img/zhankai.png" mode="widthFix"></image>
				</view>
				<view v-if="userInfo.bindWxFlag=='N'" @tap="bang" class="accountSetUpBoxItem">
					<text>微信绑定</text>
					<image src="@/static/img/zhankai.png" mode="widthFix"></image>
				</view>
				<view v-if="userInfo.bindWxFlag=='Y'" @tap="unbang" class="accountSetUpBoxItem">
					<text>解绑微信</text>
					<image src="@/static/img/zhankai.png" mode="widthFix"></image>
				</view>

				<view @tap="navToLogOff" class="accountSetUpBoxItem">
					<text>注销账号</text>
					<image src="@/static/img/zhankai.png" mode="widthFix"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { queryUserInfo, logOffUser, unBindWx, bindWx } from '@/utils/api/userInfo.ts'
	import { onLoad } from '@dcloudio/uni-app'
	import { ref } from 'vue'

	const userInfo = ref()
	const navTo = () => {
		uni.navigateTo({
			url: '/pages/reset/reset?operateType=MODIFY&phone=' + userInfo?.value?.appUser?.phone
		})
	}
	const navToPayPw = () => {
		uni.navigateTo({
			url: '/pages/reset/resetPayPw?operateType=MODIFY&phone=' + userInfo?.value?.appUser?.phone
		})
	}
	const bang = () => {
		uni.showModal({
			title: '提示',
			content: '是否确认绑定微信？',
			success: function (res) {
				if (res.confirm) {
					uni.login({
						"provider": 'weixin',
						"onlyAuthorize": true, //使用微信登录
						success: async (loginRes) => {
							const loginData = {
								code: loginRes.code,
								loginType: 'WX_LOGIN'
							}
							const res = await bindWx({
								data: loginData
							})
							doQueryUserInfo()
							uni.showToast({
								title: '绑定成功!',
								icon: 'success'
							});
						},
						fail: async (err) => {
							if (err.code == -8) {
								uni.showToast({
									title: '手机未安装微信!',
									icon: 'error'
								});
							}

						}
					});
				} else if (res.cancel) {
					console.log('用户点击取消');
					// 这里执行取消后的操作
				}
			}
		});
		// uni.showActionSheet({
		// 	itemList: ['绑定微信'],
		// 	success: function (res) {
		// 		 uni.login({
		// 		 	"provider": 'weixin',
		// 		 	"onlyAuthorize": true, //使用微信登录
		// 		 	success: async (loginRes) => {
		// 		 		const loginData = {
		// 		 			code: loginRes.code,
		// 		 			loginType: 'WX_LOGIN'
		// 		 		}
		// 		 		const res = await bindWx({
		// 		 			data: loginData
		// 		 		})
		// 		 		doQueryUserInfo()
		// 				uni.showToast({
		// 					title: '绑定成功!',
		// 					icon: 'success'
		// 				});
		// 		 	},
		// 		 	fail: async (err) => {
		// 		 		if (err.code == -8) {
		// 		 			uni.showToast({
		// 		 				title: '手机未安装微信!',
		// 		 				icon: 'error'
		// 		 			});
		// 		 		}

		// 		 	}
		// 		 });
		// 	}
		// });

	}

	const unbang = () => {
		uni.showModal({
			title: '提示',
			content: '是否确认解绑微信？',
			success: function (res) {
				if (res.confirm) {
					unBindWx().then(res => {
						doQueryUserInfo()
					})
				} else if (res.cancel) {
					console.log('用户点击取消');
					// 这里执行取消后的操作
				}
			}
		});
		// uni.showActionSheet({
		// 	itemList: ['确定解绑',],
		// 	success: function (res) {
		// 		 unBindWx().then(res=>{
		// 			 doQueryUserInfo()
		// 		 })
		// 	}
		// });

	}
	const logoutAccount = () => {
		uni.showModal({
			title: '提示',
			content: '是否确认注销？',
			success: function (res) {
				if (res.confirm) {
					logOffUser().then(res => {
						uni.showToast({
							title: '注销成功',
							icon: 'success'
						});
						uni.clearStorage()
						uni.navigateTo({
							url: '/pages/login/login'
						});
					})
				} else if (res.cancel) {
					console.log('用户点击取消');
					// 这里执行取消后的操作
				}
			}
		});
		// uni.showActionSheet({
		// 	itemList: ['确认注销'],
		// 	success: function (res) {
		// 		logOffUser().then(res=>{
		// 			uni.showToast({
		// 				title: '注销成功',
		// 				icon: 'success'
		// 			});
		// 			uni.clearStorage()
		// 			uni.navigateTo({
		// 				url: '/pages/login/login'
		// 			});
		// 		})
		// 	}
		// });
	}

	const navToLogOff = () => {
		uni.navigateTo({
			url: '/pages/logOff/logOff'
		})
	}

	const doQueryUserInfo = () => {
		queryUserInfo().then(res => {
			userInfo.value = res.data
		})

	}
	doQueryUserInfo()
</script>

<style lang="scss" scoped>
	.index {
		height: 100vh;
		background-color: #f4f6fa;

		.accountSetUp {
			padding: 24rpx;

			.accountSetUpBox {
				background-color: #fff;
				height: 318rpx;
				border-radius: 24rpx;
				padding: 24rpx;

				.accountSetUpBoxItem {
					height: 74rpx;
					padding: 16rpx;
					font-size: 28rpx;
					color: #262626;
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-bottom: 24rpx;

					image {
						width: 36rpx;
					}
				}
			}
		}
	}
</style>